package cn.wolfcode.education.service.impl;

import cn.wolfcode.education.domain.Collections;
import cn.wolfcode.education.domain.Employee;
import cn.wolfcode.education.domain.RegularStu;
import cn.wolfcode.education.mapper.CollectionsMapper;
import cn.wolfcode.education.mapper.RegularStuMapper;
import cn.wolfcode.education.query.QueryObject;
import cn.wolfcode.education.service.ICollectionsService;
import cn.wolfcode.education.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class CollectionsServiceImpl implements ICollectionsService {

    @Autowired
    private CollectionsMapper collectionsMapper;
    @Autowired
    private RegularStuMapper regularStuMapper;

    public void insert(Collections collections) {
        collectionsMapper.insert(collections);
    }

    public void update(Collections collections) {
        //如果当前学员的状态为正常
            //修改付款管理表中的缴费信息
            collectionsMapper.updateByPrimaryKey(collections);
            RegularStu record = new RegularStu();
            //设置当前缴费正式学员的id值
            record.setId(collections.getRegularStuId());
            //设置当前缴费正式学员的总费用
            record.setTotalFee(collections.getTotalFee());
            //设置当前缴费正式学员的未缴费用
            record.setUnPaidFee(collections.getUnPaidFee());
            //设置当前缴费正式学员的已缴费用
            record.setPaidFee(collections.getPaidFee());
            //设置当前缴费正式学员的支付方式
            record.setPayment(collections.getPayment());
            //设置当前缴费正式学员的缴费类型
            record.setPaymentType(collections.getPaymentType());
            //如果当前缴费正式学员的未缴费用为0
            if (collections.getUnPaidFee() == new BigDecimal("0")) {
                //则设置当前正式学员的费用状态为已缴清
                record.setFeeStatus(1);
            } else {
                //否则设置为未缴清
                record.setFeeStatus(0);
            }
            //更新正式学员中的缴费信息
            regularStuMapper.updateCollections(record);
    }

    public PageResult query(QueryObject qo) {
        Integer total = collectionsMapper.queryForCount(qo);
        if(total == 0){
            return new PageResult();
        }
        List<Collections> rows = collectionsMapper.queryForList(qo);
        return new PageResult(total,rows);
    }

    public void auditCollections(Long id) {
        //审核操作 添加审核人
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        collectionsMapper.auditCollections(id,employee.getId());
    }

    public void unauditCollections(Long id) {
        collectionsMapper.unauditCollections(id);
    }
}
